package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Report;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service(version = "1.0.0")
public class ReportApiImpl implements ReportApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新报告
     *
     * @param userId 用户id
     * @param star   星别
     * @param score  得分
     */
    public String saveReportList(Long userId, Integer star, Integer score) {
        //查询mongo中用户id对应的星别是否存在
        Report scoreByStar = findScoreByStar(userId, star);
        //如果对象为空,说明不存在 则进行添加
        if (scoreByStar == null) {
            //创建Report对象
            Report report = new Report();
            report.setId(ObjectId.get());  //设置id
            report.setCreated(System.currentTimeMillis()); //创建时间
            report.setStar(star); //星别
            report.setUserId(userId); //用户id

            switch (star) {
                case 2: {
                    report.setIsLock(0);
                    break;
                }
                case 3: {
                    //拿到初级的报告得分,如果得分大于0,说明初级题做完了,可以解锁
                    Report report2 = findScoreByStar(userId, 2);
                    if (report2.getScore() > 0) {
                        report.setIsLock(0);
                    }
                    break;
                }
                case 5: {
                    //拿到中级的报告得分,如果得分大于0,说明中级题做完了,可以解锁
                    Report report3 = findScoreByStar(userId, 3);
                    if (report3.getScore() > 0) {
                        report.setIsLock(0);
                    }
                    break;
                }
            }
            //添加到mongodb中
            mongoTemplate.save(report);
            return report.getId().toHexString();
        } else {

            if (score > 0) {
                if (star==2){
                    //设置初级的分值,然后将中级的题目解锁
                    scoreByStar.setScore(score);
                    mongoTemplate.save(scoreByStar);
                    Report report = findScoreByStar(userId, 3);
                    report.setIsLock(0);
                    mongoTemplate.save(report);
                }else if (star==3){
                    //传入的星别为3及是中级问卷时，先将分数存入mongodb中
                    scoreByStar.setScore(score);
                    mongoTemplate.save(scoreByStar);
                    //获取到高级问卷的报告
                    Report report = findScoreByStar(userId, 5);
                    //解锁
                    report.setIsLock(0);
                    mongoTemplate.save(report);
                }else {
                    scoreByStar.setScore(score);
                    mongoTemplate.save(scoreByStar);
                }
            }
            return scoreByStar.getId().toHexString();
        }
    }


    /**
     * 根据用户id和星别查询对应的报告表,返回对应的Report对象
     *
     * @param userId 用户id
     * @param star   星别
     * @return
     */
    @Override
    public Report findScoreByStar(Long userId, Integer star) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("star").is(star));
        Report report = mongoTemplate.findOne(query, Report.class);
        //不为空,返回对象
        if (report != null) {
            return report;
        }
        //为空,返回null
        return null;
    }

    /**
     * 根据用户id查询用户是否存在
     * @param userId
     * @return
     */
    public Boolean queryReportList(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Report> reportList = mongoTemplate.find(query, Report.class);
        if (reportList != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据报告id查询报告及对应的星别
     * @param reportId
     * @return
     */
    @Override
    public Report queryReportOne(String reportId) {
        return  mongoTemplate.findById(new ObjectId(reportId),Report.class);
    }


    /**
     * 根据星级及分数查询匹配的报告集合
     * @param star
     * @return
     */
    @Override
    public List<Report> queryRecommendReprt(Integer star) {
        Pageable pageable = PageRequest.of(0, 10);
        Query query =Query.query(Criteria.where("star").is(star).and("Score").gt(0)).with(pageable);
        List<Report> reportList = mongoTemplate.find(query, Report.class);
        return reportList;
    }


}
